<?php
require_once dirname(__FILE__) . '/inc.head.php';
$SIZE = 20;

$my_order = MiscUtils::getParam('my_order', 0);
$page = MiscUtils::getParam('pg', 1);
$instance_id = MiscUtils::getParam('inid', 0);

$order = NULL;
$orders = new stdClass();
if ($my_order == 0) {
  if ($status == 1) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.customer_id = :v1 AND is_paid = 0 AND is_cancel = 0';
    $cond_vals->v = array(':v1' => $_SESSION['index' . USER]->id);
    $orders = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, 't.id', 'DESC', $page, $SIZE, NULL);
  } else if ($status == 2) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.customer_id = :v1 AND is_paid = 1 AND is_delivered = 0 AND is_cancel = 0';
    $cond_vals->v = array(':v1' => $_SESSION['index' . USER]->id);
    $orders = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, 't.id', 'DESC', $page, $SIZE, NULL);
  } else if ($status == 3) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.customer_id = :v1 AND is_delivered = 1 AND date_deliver >= :v2 AND is_cancel = 0';
    $cond_vals->v = array(':v1' => $_SESSION['index' . USER]->id, ':v2' => date('Y-m-d', time() - (864000)));
    $orders = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, 't.id', 'DESC', $page, $SIZE, NULL);
  } else if ($status == 4) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.customer_id = :v1 AND is_delivered = 1 AND date_deliver < :v2 AND is_cancel = 0';
    $cond_vals->v = array(':v1' => $_SESSION['index' . USER]->id, ':v2' => date('Y-m-d', time() - (864000)));
    $orders = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, 't.id', 'DESC', $page, $SIZE, NULL);
  }
} else {
  $parent_status = 'Unpaid';
  if ($status == 2) {
    $parent_status = 'Paid';
  } else if ($status == 3) {
    $parent_status = 'Shipped';
  } else if ($status == 4) {
    $parent_status = 'Completed';
  }
  
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<script src="../../../libs/jquery/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="js/sliding_effect.js"></script>
<script type="text/javascript" src="js/jquery.simplePagination.js"></script>
<link type="text/css" rel="stylesheet" href="css/simplePagination.css"/>

<script>
$(function() {
  <?php if ($my_order != 0) { ?>
  <?php } else { ?>
  $('#pagination').pagination({
    items: <?php echo $orders->t; ?>,
    itemsOnPage: <?php echo $orders->s; ?>,
    currentPage: <?php echo $orders->p; ?>,
    cssStyle: 'compact-theme',
    onPageClick: function(pageNumber, event) {
      parent.location.href = 'http://<?php echo URL . '?i=' . $page_id . '&script=' . $script . '&pg='; ?>' + pageNumber;
    }
  });
  <?php } ?>
});
</script>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
}
table.form {
  background-color: #fff;
  border-top: 1px solid #aaa;
  border-left: 1px solid #aaa;
  width: 400px;
}
table.form td {
  padding: 3px 5px; 
  vertical-align: top;
}
table.form td.title {
  background-color: #ddd !important;
  border-bottom: 1px solid #aaa;
  border-right: 1px solid #aaa;
  width: 100px;
}
table.form td.content {
  padding: 0;
  border-bottom: 1px solid #aaa;
  border-right: 1px solid #aaa;
}
table.form td.content input {
  width: 98%;
  height: 20px;
  border: 0;
  padding-left: 2px;
}
table.lo {
  width: 100%;
}
table.lo td {
  vertical-align: top;
}
.imgbtn {
  background-image: url(img/save.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
  font-family: Lucida Sans Unicode;
}
.imgbtn:hover {
  background-color: #eee;
}
table.list {
  border-left: 1px solid #aaa;
  border-top: 1px solid #aaa;
  font-size: 13px;
  width: 100%;
  margin-top: 18px;
}
table.list th.sort {
  text-align: left;
  height: 28px;
  background-color: #ddd;
  padding: 0 5px; 
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  font-weight: bold;
  vertical-align: middle;
  cursor: pointer;
  color: #333;
}
table.list tr.rowodd td {
  padding: 0 5px;
  height: 25px;
  background-color: #eee;
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  word-wrap: break-word;
  vertical-align: middle;
  overflow: hidden;
}
table.list tr.roweven td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.rowodd:hover td {
  background-color: #f4f3b2;
}
table.list tr.roweven:hover td {
  background-color: #f4f3b2;
}
table.list tr.none td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
}
table.list input[type=text] {
  width: 99%;
  border: none;
}
div.wrap {
  padding: 5px 15px 25px 15px;
  background: #eee;
  border: 1px solid #ddd;
  width: 740px;
}
.img {
  background:url(img/delete24.png) no-repeat;
  cursor:pointer;
  border: none;
  width: 100%;
  height: 24px;
  margin-top: 5px;
}
.view {
  background:url(img/find.png) no-repeat !important;
}
.pay {
  background:url(img/pay.png) no-repeat !important;
}
.pay24 {
  background:url(img/pay24.png) no-repeat !important;
}
.cancel {
  background:url(img/del24.png) no-repeat !important;
}
form {
  padding: 0;
  margin: 0;
}
.imgbtn {
  background-image: url(img/back.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
  font-family: Lucida Sans Unicode;
}
.imgbtn:hover {
  background-color: #eee;
}
</style>
</head>
<body>
<table class="lo" cellpadding=0 cellspacing=0>
  <tr>
    <td style="width: 200px;"><?php include dirname(__FILE__) . '/inc.left.php'?></td>
    <td style="padding-left: 20px;">
      <?php if ($my_order != 0) { ?>
      <table cellpadding=0 cellspacing=0>
        <tr>
          <td>
            <a href="#" style="color:black;" onclick="parent.location.href=parent.document.URL.replace('&my_order=<?php echo $my_order; ?>', '');"><?php echo $parent_status; ?> Orders</a>
          </td>
          <td style="padding: 0 10px;">&#8250;</td>
          <td>Order Detail</td>
        </tr>
      </table>
      <div>&nbsp;</div>
      <table cellpadding=0 cellspacing=0>
        <tr>
          <td><input type='submit' value="Back to <?php echo $parent_status; ?> Orders" class="imgbtn" onclick="parent.location.href=parent.document.URL.replace('&my_order=<?php echo $my_order; ?>', '');"></input></td>
          <?php if ($status == 1) { ?>
          <td style="padding-left: 5px;"><input type='submit' value="Pay Now" class="imgbtn pay24" onclick="window.open('http://<?php echo URL . '?i=' . HOME_PROD_ID . '&script=index_paynow&pay=' . $my_order; ?>');"></input></td>
          <td style="padding-left: 5px;">
            <form id="cancel_<?php echo $my_order; ?>" action="<?php echo basename(__FILE__, '.php'); ?>.server.php" method="post">
              <input name="action" value="cancel" style="display: none;"></input>
              <input name="id" value="<?php echo $my_order; ?>" style="display: none;"></input>
              <input name="to" value="http://<?php echo URL . '?i=' . $page_id . '&x=1&script=index_order&status=' . $status; ?>" style="display: none;"></input>
              <input type='submit' value="Cancel Order" class="imgbtn cancel"></input>
            </form>
            <script>
            $(function(){
              $('#cancel_<?php echo $my_order; ?>').submit(function(e) {
                if (!window.confirm('Are You Sure?')) {
                  e.preventDefault();
                  return false;
                }
              });
            });
            </script>
          </td>
          <?php } ?>
        </tr>
      </table>
      <div id="dev_order" style="margin: 5px 0;">
        <script type="text/javascript">
          function update_height() {
            var ifm = document.getElementById("iframe_order");
            var dev = document.getElementById("dev_order");
            if (ifm && !window.opera){
              if (ifm.contentDocument && ifm.contentDocument.body){
                var body = ifm.contentDocument.body;
                var html = ifm.contentDocument.documentElement;
                ifm.height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
                dev.style.height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight) + 'px';
              } else if (document.all('iframe_order') && iframe_order.document.body) {
                document.all('iframe_order').height = iframe_order.document.body.scrollHeight;
              }
            }
            parent.updateHeight_<?php echo $instance_id; ?>();
          }
        </script>
        <iframe id="iframe_order" onload="update_height()" width="100%" scrolling="no" frameborder="0"  marginwidth="0" marginheight="0" allowtransparency="true" src="../../../incls/php/ecomorder.print.php?id=<?php echo $my_order; ?>"></iframe>
      </div>
      <table cellpadding=0 cellspacing=0>
        <tr>
          <td><input type='submit' value="Back to <?php echo $parent_status; ?> Orders" class="imgbtn" onclick="parent.location.href=parent.document.URL.replace('&my_order=<?php echo $my_order; ?>', '');"></input></td>
          <?php if ($status == 1) { ?>
          <td style="padding-left: 5px;"><input type='submit' value="Pay Now" class="imgbtn pay24" onclick="window.open('http://<?php echo URL . '?i=' . HOME_PROD_ID . '&script=index_paynow&pay=' . $my_order; ?>');"></input></td>
          <td style="padding-left: 5px;">
            <form action="<?php echo basename(__FILE__, '.php'); ?>.server.php" method="post">
              <input name="action" value="cancel" style="display: none;"></input>
              <input name="id" value="<?php echo $my_order; ?>" style="display: none;"></input>
              <input name="to" value="http://<?php echo URL . '?i=' . $page_id . '&x=1&script=index_order&status=' . $status; ?>" style="display: none;"></input>
              <input type='submit' value="Cancel Order" class="imgbtn cancel"></input>
            </form>
          </td>
          <?php } ?>
        </tr>
      </table>
      <?php } else { ?>
      <table class="list" cellpadding=0 cellspacing=0>
        <tr>
          <th class="sort" style="width: 100px;">ORDER NO.</th>
          <th class="sort" style="width: 60px;">DATE</th>
          <th class="sort" style="width: 60px;">TOTAL</th>
          <th class="sort">ADDRESS</th>
          <th class="sort" style="width: 80px;">PAYMENT</th>
          <th class="sort" style="<?php if ($status == 1) { ?>border-right: none;<?php } ?>width: 16px;padding: 0 2px;">&nbsp;</th>
          <?php if ($status == 1) { ?>
          <th class="sort" style="border-right: none;width: 20px;padding: 0 2px;">&nbsp;</th>
          <th class="sort" style="width: 16px;padding: 0 2px;">&nbsp;</th>
          <?php } ?>
        </tr>
        <?php foreach ($orders->d as $idx => $od) { ?>
        <tr class="<?php echo (($idx % 2 == 0) ? 'roweven' : 'rowodd'); ?>">
          <td><?php echo $od->order_number; ?></td>
          <td><?php echo $od->date; ?></td>
          <td>$<?php echo $od->total; ?></td>
          <td><?php echo $od->address1; ?></td>
          <td><?php echo $od->payment_name; ?></td>
          <td style="<?php if ($status == 1) { ?>border-right: none;<?php } ?>padding: 0 2px;"><input title="View" type='submit' value="" class="img view" onclick="parent.location.href=(parent.document.URL + '&my_order=<?php echo $od->order_number; ?>');"></input></td>
          <?php if ($status == 1) { ?>
          <td style="border-right: none;padding: 0 2px;"><input title="Pay Now" type='submit' value="" class="img pay" onclick="window.open('http://<?php echo URL . '?i=' . HOME_PROD_ID . '&script=index_paynow&pay=' . $od->order_number; ?>');"></input></td>
          <td style="padding: 0 2px;">
            <form id="cancel_<?php echo $od->order_number; ?>" action="<?php echo basename(__FILE__, '.php'); ?>.server.php" method="post">
              <input name="action" value="cancel" style="display: none;"></input>
              <input name="id" value="<?php echo $od->order_number; ?>" style="display: none;"></input>
              <input name="to" value="http://<?php echo URL . '?i=' . $page_id . '&x=1&script=index_order&status=' . $status; ?>" style="display: none;"></input>
              <input title="Cancel" type='submit' value="" class="img"></input>
            </form>
            <script>
            $(function(){
              $('#cancel_<?php echo $od->order_number; ?>').submit(function(e) {
                if (!window.confirm('Are You Sure?')) {
                  e.preventDefault();
                  return false;
                }
              });
            });
            </script>
          </td>
          <?php } ?>
        </tr>
        <?php } ?>
        <?php if (count($orders->d) == 0) { ?>
        <tr class="none"><td colspan=8>None</td></tr>
        <?php } ?>
      </table>
      <div>&nbsp;</div>
      <div id="pagination"></div>
      <div>&nbsp;</div>
      <?php } ?>
    </td>
  </tr>
</table>
</body>
</html>